package cn.easybuy.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.easybuy.dao.BaseDao;
import cn.easybuy.dao.NewsDao;
import cn.easybuy.entity.News;

public class NewsDaoImpl extends BaseDao implements NewsDao{

	@Override
	public List<News> getNews() {//查询所有新闻资讯  右边栏
		String sql="select * from easybuy_news order by createTime desc";
		ResultSet rs=this.exxcuteQuery(sql);
		List<News> list=new ArrayList<News>();
		try {
			while (rs.next()) {
				News news=new News();
				news.setId(rs.getInt(1));
				news.setTitle(rs.getString(2));
				news.setContent(rs.getString(3));
				news.setCreateTime(rs.getString(4));
				list.add(news);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			closeAll(connection, pstmt, rs);
		}
		return list;
	}

	@Override
	public List<News> getNewsPageList(int currNo, int pageSize) {
		String sql="select * from easybuy_news order by createTime desc limit ?,?";
		ResultSet rs=this.exxcuteQuery(sql, (currNo-1)*pageSize,pageSize);
		List<News> list=new ArrayList<News>();
		try {
			while(rs.next()){
				News news=new News();
				news.setId(rs.getInt(1));
				news.setTitle(rs.getString(2));
				news.setContent(rs.getString(3));
				news.setCreateTime(rs.getString(4));
				list.add(news);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			closeAll(connection, pstmt, rs);
		}
		return list;
	}

	@Override
	public int getTotalCount() {
		String sql="select count(1) from easybuy_news";
		ResultSet rs=this.exxcuteQuery(sql);
		int totalCount=0;
		try {
			while(rs.next()){
				totalCount=rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			closeAll(connection, pstmt, rs);
		}
		return totalCount;
	}

	@Override
	public List<News> getNewsById(int id) {
		String sql="select * from easybuy_news where id=?";
		ResultSet rs=this.exxcuteQuery(sql, id);
		List<News> list=new ArrayList<News>();
		try {
			while(rs.next()){
				News news=new News();
				news.setId(rs.getInt(1));
				news.setTitle(rs.getString(2));
				news.setContent(rs.getString(3));
				news.setCreateTime(rs.getString(4));
				list.add(news);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			closeAll(connection, pstmt, rs);
		}
		return list;
	}

}
